﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using CuteRibs.D3Armory.Models;
using CuteRibs.D3Armory.Resources;
using CuteRibs.MVVMLib;
using Newtonsoft.Json;

namespace CuteRibs.D3Armory.ViewModels
{
	public class FollowerViewModel : ViewModelBase
	{
		private bool refresh;
		private Follower follower;
		private Visibility visibility;

		private string backgroundUri;
		private string level;

		public Follower Follower
		{
			get { return this.follower; }
			set
			{
				if (this.follower != value)
				{
					this.follower = value;
					this.NotifyPropertyChanged("Follower");
					this.SetViewModel();
				}
			}
		}

		public Visibility Visibility
		{
			get { return this.visibility; }
			set
			{
				if (this.visibility != value)
				{
					this.visibility = value;
					this.NotifyPropertyChanged("Visibility");
				}
			}
		}

		public string BackgroundUri
		{
			get { return this.backgroundUri; }
			set
			{
				if (this.backgroundUri != value)
				{
					this.backgroundUri = value;
					this.NotifyPropertyChanged("BackgroundUri");
				}
			}
		}

		public string Level
		{
			get { return this.level; }
			set
			{
				if (this.level != value)
				{
					this.level = value;
					this.NotifyPropertyChanged("Level");
				}
			}
		}

		public ObservableCollection<FollowerItemViewModel> Items { get; set; }

		public ObservableCollection<SkillViewModel> SkillSets { get; set; }

		public FollowerViewModel(Follower follower, bool refresh)
			: base()
		{
			this.refresh = refresh;
			this.Follower = follower;
		}

		private void SetViewModel()
		{
			if (this.Follower == null)
			{
				return;
			}

			this.Visibility = Visibility.Visible;
			this.Level = string.Format("{0}: {1}", AppRes.Level, this.Follower.Level);
			this.SetSkills(this.Follower.Skills);

			if (!App.IsDesignMode)
			{
				this.SetItems(this.Follower.Items);
			}
		}

		private void SetSkills(SkillSet[] skills)
		{
			if (this.SkillSets == null)
			{
				this.SkillSets = new ObservableCollection<SkillViewModel>();
			}
			else
			{
				this.SkillSets.Clear();
			}

			for (int i = 0; i < skills.Length; i++)
			{
				this.SkillSets.Add(new SkillViewModel(-1, skills[i]));
			}
		}

		private async void SetItems(Follower.FollowerItems items)
		{
			if (this.Items == null)
			{
				this.Items = new ObservableCollection<FollowerItemViewModel>();
			}
			else
			{
				this.Items.Clear();
			}

			foreach (var itemBrief in items.AllItems)
			{
				try
				{
					Item item = null;

					if (refresh)
					{
						item = await App.GetItem(itemBrief);
					}
					else
					{
						item = await App.LoadItem(itemBrief);
					}

					if (!string.IsNullOrEmpty(item.ErrorCode))
					{
						App.ShowAlert(string.Format("{0}: {1}", item.ErrorCode, item.ErrorReason));
						return;
					}
					else
					{
						this.Items.Add(new FollowerItemViewModel(item));
					}
				}
				catch (Exception ex)
				{
					if (ex is WebException || ex is JsonReaderException)
					{
						App.ShowAlert(AppRes.MsgDataAccessError);
					}
					else
					{
						App.ShowError(new AppException(ex, itemBrief.TooltipParams));
					}

					return;
				}
			}
		}
	}
}
